package com.zy.dao.impl;

import com.zy.dao.RoleDao;
import com.zy.pojo.Role;
import com.zy.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2021-06-10
 * Time: 下午 20:54
 * Description: 角色持久层实现类
 */
public class RoleDaoImpl implements RoleDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public Role getRoleByEmpId(Integer empId) {
        String sql = "select * from role where empId = ?";
        Role role = null;
        try {
            role = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Role>(Role.class), empId);
        } catch (DataAccessException e) {
        } finally {
            return role;
        }
    }

    @Override
    public Integer updateRoleByEmpId(Role role) {
        String sql = "update role set roleName = ? where empId = ?";
        return jdbcTemplate.update(sql, role.getRoleName(), role.getEmpId());
    }

    @Override
    public Integer insertEmpRole(Role role) {
        String sql = "insert into role (empId , roleName) values (? , ?)";
        return jdbcTemplate.update(sql, role.getEmpId(), role.getRoleName());
    }
}